package com.app.core_app.controller;

import com.app.core_app.entity.CaseManager;
import com.app.core_app.properties.MypwProperties;
import com.app.core_app.service.CaseManagerService;
import com.app.core_app.vo.DataInfoVo;
import com.app.public_dependency.response.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("auto-test-list")
@RequiredArgsConstructor
@Slf4j
public class AutoTestListController {

    private final CaseManagerService caseManagerService;
    private final MypwProperties mypwProperties;

    /**
     * 获取列表信息并分页
     *
     * @param current  当前页数
     * @param pageSize 页面大小
     * @return
     */
    @GetMapping("queryPageInfo/{current}/{pageSize}")
    public Result queryPageInfo(@PathVariable("current") Integer current, @PathVariable("pageSize") Integer pageSize) {
        List<DataInfoVo> caseManagerList = caseManagerService.queryPageInfo(current, pageSize);

        return Result.success().data("caseManagerList", caseManagerList);
    }

    /**
     * 批量执行代码脚本
     *
     * @param keys
     * @return
     */
    @PostMapping("executeBatch")
    public Result executeBatch(@RequestBody List<String> keys) {
        return caseManagerService.executeBatch(keys);
    }

    /**
     * 下载运行结果
     *
     * @param fileName
     * @return
     */
    @GetMapping("download/{fileName}")
    public Mono<Void> download(@PathVariable String fileName, ServerHttpResponse response) throws IOException {

        // TODO

        Resource resource = new ClassPathResource(mypwProperties.getPwReportPath() + fileName);
        ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;

        response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"");
        response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);

        log.info(resource.getFilename());

        File file = resource.getFile();
        return zeroCopyResponse.writeWith(file, 0, file.length());

    }

    /**
     * 批量删除
     *
     * @param keys
     * @return
     */
    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<String> keys) {
        return caseManagerService.deleteBatch(keys);
    }

    /**
     * 修改
     * @param caseId
     * @param pgId
     * @param processId
     * @return
     */
    @PostMapping("modify/{pgId}/{processId}/{caseId}")
    public Result modify(@PathVariable String caseId, @PathVariable String pgId, @PathVariable String processId) {
        // TODO
        log.info("当前修改的是：" + pgId + " " + processId + " " + caseId);
        return Result.success();
    }

}
